package groovy.jmx.builder.test

import groovy.jmx.builder.JmxBuilder
import groovy.jmx.builder.JmxBuilderTools
import java.rmi.registry.LocateRegistry
import javax.management.MBeanServer

class JmxBeanIntegrationRun {
    public static void main(args) {
        // get hold of platform server
        MBeanServer server = (MBeanServer) JmxBuilderTools.getMBeanServer()
        JmxBuilder builder = new JmxBuilder()

        def object = new MockManagedObject()
        def beans = builder.export {
            timer(name: "jmx.builder:type=Timer", event: "heartbeat", period: "1s").start()
            timer(name: "jmx.builder:type=Timer2", event: "timer", period: "1s").start()

            bean(target: object, name: "jmx.builder:type=ExportedBean",
                    attributes: ["Something": [writable: true, onChange: {-> println "Got something"}]],
                    operations: [
                            "doSomething": [onCall: {event -> println "I did something"}],
                            "doSomethingElse": [params: ["int", "string"], onCall: {event -> "I did something else"}]
                    ],
                    listeners: [
                            heartBeatListener: [from: "jmx.builder:type=Timer", call: {event ->
                                object.doSomething()
                                println "beep... ${event.sequenceNumber}"
                            }]
                    ]
            )
        }
        LocateRegistry.createRegistry(9000)
        builder.connectorServer(port: 9000).start()

        JmxBuilder bldr = new JmxBuilder()
        def client = bldr.clientConnector(port: 9000)
        client.connect()
        bldr.setMBeanServer client.getMBeanServerConnection()
        def listener = bldr.listener(from: "jmx.builder:type=Timer2", call: {event ->
            println "time ${new Date()}"
        })
    }
}